<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>拿出最少数目的魔法豆</title>
</head>
<body>
  <script>
    function fn(beans) {
      const sum = beans.reduce((cur, next) => cur + next, 0)
      let ans = sum
      beans.sort((a, b) => a - b)
      for (let i = 0; i < beans.length; i++) {
        ans = Math.min(ans, sum - beans[i] * (beans.length - i))
      }
      return ans
    }
    console.log(fn([4,1,6,5]))
  </script>
</body>
</html>